<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <link href="http://apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
    <script src="http://apps.bdimg.com/libs/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container mt-5">
        <h1>新闻列表</h1>
        <button class="btn btn-primary mb-3" data-toggle="modal" data-target="#articleModal">新增新闻</button>
        <table class="table">
            <thead>
                <tr>
                    <th>标题</th>
                    <th>作者</th>
                    <th>发布日期</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="article-list">
                <!-- News items will be dynamically inserted here -->
            </tbody>
        </table>
    </div>

    <!-- Modal for adding/editing articles -->
    <div class="modal fade" id="articleModal" tabindex="-1" role="dialog" aria-labelledby="articleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="articleModalLabel">新增新闻</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="articleForm">
                        <input type="hidden" id="articleId">
                        <div class="form-group">
                            <label for="title">标题</label>
                            <input type="text" class="form-control" id="title" required>
                        </div>
                        <div class="form-group">
                            <label for="author">作者</label>
                            <input type="text" class="form-control" id="author" required>
                        </div>
                        <div class="form-group">
                            <label for="content">内容</label>
                            <textarea class="form-control" id="content" rows="5" required></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="saveArticle">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {

            fetchArticles();
            $('#saveArticle').click(saveArticle);
        });

        function fetchArticles() {
            $.get('/article/api/articles/', function(data) {
                $('#article-list').empty();
                data.forEach(function(article) {
                    $('#article-list').append(`
                        <tr>
                            <td>${article.title}</td>
                            <td>${article.author}</td>
                            <td>${new Date(article.published_date).toLocaleDateString()}</td>
                            <td>
                                <button class="btn btn-info btn-sm" onclick="viewArticle(${article.id})">查看</button>
                                <button class="btn btn-warning btn-sm" onclick="editArticle(${article.id})">编辑</button>
                                <button class="btn btn-danger btn-sm" onclick="deleteArticle(${article.id})">删除</button>
                            </td>
                        </tr>
                    `);
                });
            });
        }

        function saveArticle() {
            const articleId = $('#articleId').val();
            const title = $('#title').val();
            const author = $('#author').val();
            const content = $('#content').val();
            const articleData = { title, author, content };

            if (articleId) {
                $.ajax({
                    url: `/article/api/articles/${articleId}/`,
                    method: 'PUT',
                    data: JSON.stringify(articleData),
                    contentType: 'application/json',
                    success: function() {
                        $('#articleModal').modal('hide');
                        fetchArticles();
                    }
                });
            } else {
                $.ajax({
                    url: '/article/api/articles/',
                    method: 'POST',
                    data: JSON.stringify(articleData),
                    contentType: 'application/json',
                    success: function() {
                        $('#articleModal').modal('hide');
                        fetchArticles();
                    }
                });
            }
        }

        function viewArticle(id) {
            $.get(`/article/api/articles/${id}/`, function(data) {
                alert(`标题: ${data.title}\n作者: ${data.author}\n发布日期: ${new Date(data.published_date).toLocaleDateString()}\n内容: ${data.content}`);
            });
        }

        function editArticle(id) {
            $.get(`/article/api/articles/${id}/`, function(data) {
                $('#articleId').val(data.id);
                $('#title').val(data.title);
                $('#author').val(data.author);
                $('#content').val(data.content);
                $('#articleModalLabel').text('编辑新闻');
                $('#articleModal').modal('show');
            });
        }

        function deleteArticle(id) {
            if (confirm('确定要删除这篇新闻吗？')) {
                $.ajax({
                    url: `/article/api/articles/${id}/`,
                    method: 'DELETE',
                    success: function() {
                        fetchArticles();
                    }
                });
            }
        }
    </script>
</body>
</html>
